<template>
  <div class="officeSpace-content">
    <aps-form
      :config="apsForm.formConfig"
      :model="apsForm.formData"
      :formRef="apsForm.formRef"
      :labelWidth="apsForm.labelWidth"
      ref="form"
    ></aps-form>
    <lb-table
      :column="tableData.column"
      :data="tableData.data"
      border
      :height="550"
      pagination
      background
      layout="total, sizes, prev, pager, next, jumper"
      :page-sizes="[20, 40, 60, 80]"
      :pager-count="5"
      :current-page.sync="currentPage"
      :total="total"
      :page-size="pageSize"
      @size-change="handleSizeChange"
      @p-current-change="handleCurrentChange"
    >
    </lb-table>
    <component
      :is="modelName"
      :id="id"
      :dialogTitle="dialogTitle"
      :visible.sync="show"
      @close="close"
    ></component>
  </div>
</template>

<script>
import { getparkList } from "@/api/park/parkmsg.js";
import { getbuildingList } from "@/api/park/buildingmsg.js";
import { getfloorList } from "@/api/park/floormsg.js";
import { getofficeSpacelist } from "@/api/departmental/index.js";

import operate from "@/views/work/departmental/components/operate.vue";
export default {
  name: "",
  components: {
    operate,
  },
  dicts: ["room_direction"],
  data() {
    return {
      //弹窗
      id: "",
      modelName: "",
      dialogTitle: "",
      show: false,
      parkList: [],
      buildingList: [],
      floorList: [],
      apsForm: {
        formRef: "form",
        labelWidth: "auto",
        formData: {},
        formConfig: {
          formAttr: {
            inline: true,
            size: "small",
          },
          properties: {
            // parkId: {
            //   label: "园区名称:",
            //   render: (h) => {
            //     let that = this;
            //     let arr = [...that.parkList];
            //     // 数据严谨处理,处理后端数据异常情况
            //     let valNum = that.$refs?.form?.getFieldsValue().parkId;
            //     let a = [
            //       h(
            //         "el-select",
            //         {
            //           attrs: {
            //             placeholder: "请选择园区",
            //             value: valNum,
            //             clearable: true,
            //             filterable: true,
            //           },
            //           props: ["value"],
            //           on: {
            //             change: (value) => {
            //               that.$refs?.form?.setFieldValue("parkId", value);
            //               that.$refs?.form?.setFieldValue("buildId", null);
            //               that.$refs?.form?.setFieldValue("floorId", null);
            //               that.$forceUpdate();
            //             },
            //           },
            //         },
            //         [
            //           arr.map((item) => {
            //             let { label, value } = item;
            //             return h("el-option", {
            //               props: {
            //                 label,
            //                 value: value,
            //                 key: value,
            //               },
            //             });
            //           }),
            //         ]
            //       ),
            //     ];
            //     return h("div", a);
            //   },
            // },
            buildId: {
              label: "楼宇名称:",
              render: (h) => {
                let that = this;
                let arr = [...that.buildingList];
                // 数据严谨处理,处理后端数据异常情况
                let valNum = that.$refs?.form?.getFieldsValue().buildId;
                let a = [
                  h(
                    "el-select",
                    {
                      attrs: {
                        placeholder: "请选择楼宇",
                        value: valNum,
                        clearable: true,
                        filterable: true,
                      },
                      props: ["value"],
                      on: {
                        change: (value) => {
                          that.$refs?.form?.setFieldValue("buildId", value);
                          that.$refs?.form?.setFieldValue("floorId", null);
                          that.$forceUpdate();
                        },
                        "visible-change": (value) => {
                          if (value) {
                            that.requestBuildinglist();
                          }
                        },
                      },
                    },
                    [
                      arr.map((item) => {
                        let { label, value } = item;
                        return h("el-option", {
                          props: {
                            label,
                            value: value,
                            key: value,
                          },
                        });
                      }),
                    ]
                  ),
                ];
                return h("div", a);
              },
            },
            floorId: {
              label: "楼层名称:",
              render: (h) => {
                let that = this;
                let arr = [...that.floorList];
                // 数据严谨处理,处理后端数据异常情况
                let valNum = that.$refs?.form?.getFieldsValue().floorId;
                let a = [
                  h(
                    "el-select",
                    {
                      attrs: {
                        placeholder: "请选择楼层",
                        value: valNum,
                        clearable: true,
                        filterable: true,
                      },
                      props: ["value"],
                      on: {
                        change: (value) => {
                          that.$refs?.form?.setFieldValue("floorId", value);
                          that.$forceUpdate();
                        },
                        "visible-change": (value) => {
                          if (value) {
                            that.requestFloorList();
                          }
                        },
                      },
                    },
                    [
                      arr.map((item) => {
                        let { label, value } = item;
                        return h("el-option", {
                          props: {
                            label,
                            value: value,
                            key: value,
                          },
                        });
                      }),
                    ]
                  ),
                ];
                return h("div", a);
              },
            },
            roomNumber: {
              label: "房间号:",
              typeName: "el-input",
              placeholder: "请输入房间号",
            },
            roomName: {
              label: "房间名称:",
              typeName: "el-input",
              placeholder: "请输入房间名称",
            },
          },
          formSoltButton: [
            {
              label: "搜索",
              type: "primary",
              typeName: "el-button",
              size: "small",
              icon: "el-icon-search",
              click: this.handlerSearchDetail,
            },
            {
              label: "重置",
              typeName: "el-button",
              size: "small",
              icon: "el-icon-refresh-left",
              click: this.handleReset,
            },
          ],
        },
      },
      currentPage: 1,
      pageSize: 20,
      total: 0,
      tableData: {
        column: [
          // {
          //   prop: "parkName",
          //   label: "园区名称",
          //   minWidth: 100,
          //   align: "center",
          //   showOverflowTooltip: true,
          // },
          {
            prop: "buildName",
            label: "楼宇名称",
            minWidth: 100,
            align: "center",
            showOverflowTooltip: true,
          },
          {
            prop: "floorName",
            label: "楼层名称",
            minWidth: 100,
            align: "center",
            showOverflowTooltip: true,
          },
          {
            prop: "roomType",
            label: "房间类型",
            minWidth: 120,
            align: "center",
            showOverflowTooltip: true,
          },
          {
            prop: "roomNumber",
            label: "房间号",
            minWidth: 100,
            align: "center",
            showOverflowTooltip: true,
          },
          {
            prop: "roomName",
            label: "房间名称",
            minWidth: 100,
            align: "center",
            showOverflowTooltip: true,
          },
          {
            prop: "roomArea",
            label: "房间面积",
            minWidth: 100,
            align: "center",
            showOverflowTooltip: true,
            render: (h, scope) => {
              let that = this;
              let a = scope?.row?.roomArea ?? 0;
              let b = a + "m²";
              return <div>{b}</div>;
            },
          },
          {
            prop: "direction",
            label: "房间朝向",
            minWidth: 100,
            align: "center",
            showOverflowTooltip: true,
            render: (h, scope) => {
              return (
                <div>
                  <dict-tag
                    options={this.dict.type.room_direction}
                    value={scope.row.direction}
                  />
                </div>
              );
            },
          },
          {
            prop: "peopleNum",
            label: "总人数",
            minWidth: 100,
            align: "center",
            showOverflowTooltip: true,
            render: (h, scope) => {
              let that = this;
              let name = scope?.row?.peopleNum ?? 0;
              let a = (
                <span
                  class="link-type"
                  onClick={() => {
                    that.showModel("operate", scope.row, "人员组成");
                  }}
                >
                  {name}
                </span>
              );
              return <div>{a}</div>;
            },
          },
          {
            prop: "totalValue",
            label: "总用能量",
            minWidth: 100,
            align: "center",
            showOverflowTooltip: true,
            render: (h, scope) => {
              let that = this;
              let value = scope?.row?.totalValue ?? 0;
              let name = value + " kwh";
              let a = (
                <span
                  class="link-type"
                  onClick={() => {
                    that.showModel("operate", scope.row, "用能流水");
                  }}
                >
                  {name}
                </span>
              );
              return <div>{a}</div>;
            },
          },
          {
            prop: "deviceNum",
            label: "总用能设备",
            minWidth: 100,
            align: "center",
            showOverflowTooltip: true,
            render: (h, scope) => {
              let that = this;
              let name = scope?.row?.deviceNum ?? 0;
              let a = (
                <span
                  class="link-type"
                  onClick={() => {
                    that.showModel("operate", scope.row, "设备列表");
                  }}
                >
                  {name}
                </span>
              );
              return <div>{a}</div>;
            },
          },
        ],
        data: [],
      },
    };
  },
  created() {
    this.requestParklist();
    this.requestBuildinglist();
    this.requestFloorList();
  },
  mounted() {
    this.handlerSearchDetail();
  },
  methods: {
    // 园区集合
    requestParklist() {
      this.parkList = [];
      getparkList({})
        .then((res) => {
          if (res.code === 700) {
            this.$message.warning(res.msg);
            return false;
          }
          let arr = res?.rows ?? [];
          if (arr.length > 0) {
            arr.map((item) => {
              let obj = {
                label: item?.name ?? "",
                value: item?.id,
              };
              this.parkList.push(obj);
            });
          }
        })
        .catch((err) => {
          // this.$message.error("系统错误,请联系管理员!401001");
        });
    },
    // 楼宇集合
    requestBuildinglist() {
      this.buildingList = [];
      let params = {};
      let parkId = this.$refs?.form?.getFieldsValue().parkId;
      if (String(parkId).length > 0) {
        params.parkId = parkId;
      }
      getbuildingList(params)
        .then((res) => {
          if (res.code === 700) {
            this.$message.warning(res.msg);
            return false;
          }
          let arr = res?.rows ?? [];
          if (arr.length > 0) {
            arr.map((item) => {
              let obj = {
                label: item?.name ?? "",
                value: item?.id,
              };
              this.buildingList.push(obj);
            });
          }
        })
        .catch((err) => {
          // this.$message.error("系统错误,请联系管理员!402001");
        });
    },
    // 楼层集合
    requestFloorList() {
      this.floorList = [];
      let params = {};
      let parkId = this.$refs?.form?.getFieldsValue().parkId;
      let buildId = this.$refs?.form?.getFieldsValue().buildId;
      if (String(parkId).length > 0) {
        params.parkId = parkId;
      }
      if (String(buildId).length > 0) {
        params.buildId = buildId;
      }
      getfloorList(params)
        .then((res) => {
          if (res.code === 700) {
            this.$message.warning(res.msg);
            return false;
          }
          let arr = res?.rows ?? [];
          if (arr.length > 0) {
            arr.map((item) => {
              let floorName = item?.name ?? "";
              let buildingName = item?.buildingName ?? "";
              let obj = {
                label: buildingName + "-" + floorName,
                value: item?.id,
              };
              this.floorList.push(obj);
            });
          }
        })
        .catch((err) => {
          // this.$message.error("系统错误,请联系管理员!403001");
        });
    },
    async initData(data) {
      let that = this;
      let query = {
        ...data,
      };
      query.pageNum = that.currentPage;
      query.pageSize = that.pageSize;
      that.$store.commit("app/updateApiCount", "add");
      await getofficeSpacelist(query)
        .then((res) => {
          that.$store.commit("app/updateApiCount");
          if (res.code === 700) {
            this.$message.warning(res.msg);
            return false;
          }
          that.tableData.data = res?.rows ?? [];
          that.total = res?.total ?? 0;
        })
        .catch((err) => {
          that.$store.commit("app/updateApiCount");
          // this.$message.error("系统错误,请联系管理员!");
        });
    },
    // 搜索
    handlerSearchDetail() {
      const { ...other } = this.$refs?.form?.getFieldsValue();
      const data = {
        ...other,
      };
      this.initData(data);
    },
    // 重置
    handleReset() {
      this.$refs?.form?.resetFields();
      this.handlerSearchDetail();
    },
    handleSizeChange(val) {
      this.currentPage = 1;
      this.pageSize = val;
      this.handlerSearchDetail();
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.handlerSearchDetail();
    },
    // 弹窗
    showModel(name, params, title) {
      this.modelName = name;
      this.id = params?.id || "";
      this.dialogTitle = title;
      setTimeout(() => {
        this.show = true;
      }, 200);
    },
    close(status) {
      this.show = false;
      setTimeout(() => {
        if (status) this.handlerSearchDetail();
      }, 200);
    },
  },
};
</script>

<style lang="scss" scoped>
.officeSpace-content {
  margin-top: 20px;
}
</style>
